package com.goodedeeds.domain.resource;

import com.youtills.core.lang.CollectionUtil;
import com.youtills.core.lang.ObjectUtil;
import com.youtills.domain.contact.Address;
import com.youtills.resource.*;
import com.youtills.action.ActionContext;

import java.util.Arrays;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: rchiluve
 * Date: 2/23/14
 * Time: 7:48 PM
 * To change this template use File | Settings | File Templates.
 */
public class School extends BaseResource implements Resource {

    public static final ResourceType<School> RT_SCHOOL = ResourceType.getResourceType(School.class);

    private final ResourceAttributeValue<String> schoolKey;

    private final ResourceAttributeValue<String> schoolName;

    private final ResourceAttributeValue<String> schoolDescription;

    private final ResourceAttributeValue<Integer> schoolStatus;

//    private final ResourceAttributeValue<List<Project>> projects;

    private final ResourceRelationship<School, Project> schoolProjects;
    private final ResourceRelationship<School, Address> schoolAddress;
    private Address address;

    public School() {
        super(RT_SCHOOL);
        schoolKey = new ResourceAttributeValue<>(this, CommonAttribute.key);
        schoolName = new ResourceAttributeValue<>(this, CommonAttribute.name);
        schoolDescription = new ResourceAttributeValue<>(this, CommonAttribute.description);
        schoolStatus = new ResourceAttributeValue<>(this, CommonAttribute.status_type);

        schoolProjects = new ResourceRelationship<>(ResourceRelationship.SourceLocation.left, this, null, GoodeDeedsAttribute.school_project);
        schoolAddress = new ResourceRelationship<>(ResourceRelationship.SourceLocation.left, this, null, GoodeDeedsAttribute.school_address);
    }

    public void addProject(long projectId) {
        schoolProjects.addTargetResourceId(Project.RT_PROJECT, projectId, null);
    }

    public List<Project> getProjects(ActionContext ac) {
        Long[] projectIds = schoolProjects.getTargetResourceIds();
        return ResourceManager.instance.getResourcesBulk(ac, Project.class, projectIds);
    }

    public void setSchoolAddress(Address address) {
        schoolAddress.addTargetResourceId(Address.RT_ADDRESS, address.getAddressId(), null);
        this.address = address;
    }

    public Address getAddress(ActionContext ac) {
        if(address == null) {
            Long[] addressIds = schoolAddress.getTargetResourceIds();
            if(CollectionUtil.isNotEmpty(addressIds)){
                address = ResourceManager.instance.getResource(ac, Address.class, addressIds[0]);
            }
            if(address == null) {
                address = Address.NULL_ADDRESS;
            }
        }
        return address == Address.NULL_ADDRESS ? null : address;
    }

    public Long getSchoolId() {
        return getResourceId();
    }

    public String getSchoolName() {
        return schoolName.getValue();
    }

    public void setSchoolName(String schoolName) {
        this.schoolName.setValue(schoolName);
    }

    public String getSchoolDescription() {
        return schoolDescription.getValue();
    }

    public void setSchoolDescription(String schoolDescription) {
        this.schoolDescription.setValue(schoolDescription);
    }

    public int getSchoolStatus() {
        return schoolStatus.getValue();
    }

    public void setSchoolStatus(int schoolStatus) {
        this.schoolStatus.setValue(schoolStatus);
    }

    public String getSchoolKey() {
        return schoolKey.getValue();
    }

    public void setSchoolKey(String schoolKey) {
        this.schoolKey.setValue(schoolKey);
    }

    @Override
    protected String getTableName() {
        return "ged__school";
    }

    @Override
    protected String getResourceIdColumn() {
        return "school_id";
    }

    public List<Long> getProjectIds() {
        return Arrays.asList(schoolProjects.getTargetResourceIds());
    }
}
